package cn.t.web.ent;

import cn.t.constants.ResponseError;
import cn.t.core.exception.BizException;
import cn.t.core.mode.PageInfoDTO;
import cn.t.core.mode.PageQueryDTO;
import cn.t.dto.order.DislodgeOrderDTO;
import cn.t.dto.order.OrderQueryRequest;
import cn.t.dto.order.base.OrderSnRequest;
import cn.t.dto.order.base.VBaseDTO;
import cn.t.dto.order.excelresp.DislodgeExcelResp;
import cn.t.enums.order.OrderTypeEnum;
import cn.t.facade.order.OrderFacade;
import cn.t.facade.user.RoleInfoFacade;
import cn.t.listener.CustomCellWriteWidthConfig;
import cn.t.utils.ExcelUtil;
import cn.t.vo.order.DislodgeOrderDetailVO;
import com.alibaba.excel.EasyExcelFactory;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 企业站-领用单管理
 */
@Slf4j
@RestController
@RequestMapping("/ent/order")
@AllArgsConstructor
public class EntOrderController {
    private final OrderFacade orderFacade;
    private final RoleInfoFacade roleInfoFacade;

    /**
     * 领用列表查询接口
     */
    @PostMapping(value = "/page")
    public PageInfoDTO<DislodgeOrderDTO> getList(@Validated @RequestBody PageQueryDTO<OrderQueryRequest> request) {
        if (request.getParam().getRoleId() != null) {
            // 查询权限组下的领料员
            List<Long> userIds = roleInfoFacade.getRoleUsers(request.getParam().getRoleId());
            request.getParam().setUserIds(userIds);
        }

        return orderFacade.getOrderListPage(request, OrderTypeEnum.ORDER);
    }

    /**
     * 领用记录导出
     */
    @PostMapping("/export")
    public void getListExcel(@RequestBody OrderQueryRequest request,HttpServletResponse response) {
            List<DislodgeExcelResp> dislodgeExcelVO = orderFacade.openRecordExcel(request);
            if (ObjectUtils.isEmpty(dislodgeExcelVO)) {
                throw new BizException(ResponseError.EXCEL_EMPTY, ResponseError.EXCEL_EMPTY.getName());
            }
        try {
            ExcelUtil.setHeader(response,  URLEncoder.encode("领用列表", "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            log.warn("领用列表导出异常 {}",e.getMessage());
            throw new BizException(ResponseError.EXCEL_EXPORT_ERR, ResponseError.EXCEL_EXPORT_ERR.getName());
        }
        // 执行导出
            try(ServletOutputStream outputStream = response.getOutputStream()) {

                EasyExcelFactory.write(outputStream, DislodgeExcelResp.class).sheet("领用列表").registerWriteHandler(new CustomCellWriteWidthConfig()).doWrite(dislodgeExcelVO);
            } catch (Exception e) {
                log.warn("领用记录导出 {}",e.getMessage());
                throw new BizException(ResponseError.EXCEL_EXPORT_ERR, ResponseError.EXCEL_EXPORT_ERR.getName());
            }

    }

    /**
     * 领用商品详情查询接口
     */
    @GetMapping(value = "/detail")
    public VBaseDTO<DislodgeOrderDTO, DislodgeOrderDetailVO> detail(@RequestParam String orderSn) {
        return orderFacade.getDislodgeInfo(new OrderSnRequest(orderSn), OrderTypeEnum.ORDER);
    }
}

